package com.thread.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author wangyingjie
 * @date
 * @remark
 */
public class LockTest {
          int i = 0;
    public static void main(String[] args) throws InterruptedException {

//        Lock lock = new NeteaseLock();
        Lock lock = new ReentrantLock();
        LockTest lockTest = new LockTest();

        Thread[] threads = new Thread[7];

        for (int j = 0; j < threads.length; j++) {
            int final1 = j;
            Thread thread = new Thread(()->{
                System.out.println("线程【"+final1+"】执行开始");
                for (int k = 0; k < 10000; k++) {
                    lock.lock();
                    try {
                        lockTest.i++;
                    }finally {
                        lock.unlock();
                    }
                }
                System.out.println("线程【"+final1+"】执行结束");
            });
            threads[j] = thread;
            thread.start();
        }

        for (Thread thread : threads) {
            thread.join();//当前主线程挂起，直到该线程执行结束，当前主线程才能继续执行
        }
        System.out.println("执行结束【"+lockTest.i+"】");
    }
}
